package org.lys.jdbc.beta.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.lys.jdbc.JdbcUtils;
import org.lys.jdbc.beta.RowMapper;
import org.lys.jdbc.beta.dao.BaseDao;
import org.lys.jdbc.dao.DaoException;

public class BaseDaoImpl implements BaseDao{
	/*
	 * (non-Javadoc)   
	 * @see org.lys.jdbc.beta.dao.BaseDao#executeUpdate(java.lang.String, java.lang.Object[])
	 */
	public int executeUpdate(String sql,Object[] params){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql);
			for (int i = 0;params!=null&& i < params.length; i++)
				ps.setObject(i + 1, params[i]);
			return ps.executeUpdate();
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
	}
	/*
	 * (non-Javadoc)   
	 * @see org.lys.jdbc.beta.dao.BaseDao#update(java.lang.String, java.lang.Object[])
	 */
	public int update(String sql,Object[] params){
		return this.executeUpdate(sql, params);
	}
	/*
	 * (non-Javadoc)   
	 * @see org.lys.jdbc.beta.dao.BaseDao#delete(java.lang.String, java.lang.Object[])
	 */
	public int delete(String sql,Object[] params){
		return this.executeUpdate(sql, params);
	}
	/*
	 * (non-Javadoc)   
	 * @see org.lys.jdbc.beta.dao.BaseDao#add(java.lang.String, java.lang.Object[])
	 */
	public int add(String sql,Object[] params){
		return this.executeUpdate(sql, params);
	}
	/*
	 * (non-Javadoc)   
	 * @see org.lys.jdbc.beta.dao.BaseDao#findForObject(java.lang.String, java.lang.Object[], org.lys.jdbc.beta.RowMapper)
	 */
	public Object findForObject(String sql,Object[] params,RowMapper rowMappler){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql);
			for (int i = 0; params!=null&& i< params.length; i++)
				ps.setObject(i + 1, params[i]);
			rs = ps.executeQuery();
			Object obj = null;
			if (rs.next()) {
				obj = rowMappler.rowMapper(rs);
			}
			return obj;
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
	}
	/*
	 * (non-Javadoc)   
	 * @see org.lys.jdbc.beta.dao.BaseDao#addRtnPk(java.lang.String, java.lang.Object[])
	 */
	@Override
	public Object addRtnPk(String sql, Object[] params) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			for (int i = 0;params!=null&& i < params.length; i++)
				ps.setObject(i + 1, params[i]);
			ps.executeUpdate();
			rs = ps.getGeneratedKeys();
			if (rs.next())
				return rs.getObject(1);
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
		return null;
	}
}
